import { FilterRule, ButtonIsActive, ButtonFilterDependency } from "./../common/globalType";
import { reactive } from "@vue/composition-api";

// 导出组合函数
export default function (filterDep: ButtonFilterDependency) {
    // 选择按钮当前状态
    const selectButtonStatus = reactive<ButtonIsActive>({
        all: true,
        recommend: false,
        notrecommend: false,
    });
    // 处理选择按钮点击事件
    const handleSelectButtonClick =
        function <T extends Function, U>(rule: FilterRule, callbackAfterClick?: T, ...args: U[]) {
            // 清空按钮激活样式
            Object.keys(selectButtonStatus).forEach((key) => {
                selectButtonStatus[key] = false;
            });
            filterDep.currRule = rule; // 设置过滤规则
            if (rule === 0) {
                selectButtonStatus.recommend = true;
            } else if (rule === 1) {
                selectButtonStatus.notrecommend = true;
            } else if (rule === null) {
                selectButtonStatus.all = true;
            }
            // 若存在传入回调函数，执行传入的回调函数
            if (callbackAfterClick && args) callbackAfterClick(...args);
        };
    return { handleSelectButtonClick, selectButtonStatus };
}
